import Vue from 'vue'
import VueRouter from 'vue-router'
import chenkong from "./routerlist/chenkong.js"
import cheyi from './routerlist/cheyi.js'
import mayingshuang from "./routerlist/mayingshuang.js"
import yangchao from "./routerlist/yangchao.js"
import yangchaung from "./routerlist/yangchaung.js"
import zahngyifan from "./routerlist/zahngyifan.js"

Vue.use(VueRouter)

const routes = [
  ...chenkong,
  ...cheyi,
  ...mayingshuang,
  ...yangchao,
  ...yangchaung,
  ...zahngyifan,
  {
    path: '/',
    redirect: '/homeindex',
    component: () => import('../views/CheyView/HomeIndex.vue')
  },
  // 404时出现的界面-------匹配所有路径所以要在最后配置
  {
    path: "*",
    name: "notfound",
    component: () => import('@/views/NotFound.vue')
  }
]
const router = new VueRouter({
  // history刷新数据会丢失，要用hash
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 全局路由守卫
router.beforeEach((to, from, next) => {
  var token = localStorage.getItem("token");
  document.title = to.meta.title;
  if (to.path == "/login") {//如果想去登录页
    if (token) {//如果有了token
      next("/mineself");//就留在当前页面
    } else {//没有token就去登录
      next();
    }
    //如果想去：我的主页, +addmore, 收藏界面 , 抢购页面
  } else if (to.path == "/mineself" || to.path == "/likeindex" || to.path == "/moreindex"
    || to.path == "/BuyV" || to.path == "/xiyubuy" || to.path == "/sshoppingcart" || to.path == "/threeye" || to.path == "/yacShopPage") {
    if (token) {//有token就去
      next();
    } else {//没有就先去登录
      next("/login");
    }
  } else {//如果是去普通的页面
    next();
  }
})
export default router
